#pragma once
#include "UserLatestMsgNumNotify.h"
#include<filesystem>
#include<fstream>
#include<thread>
#include<iostream>
#include<mutex>
using namespace std;
namespace fs=filesystem;

using ULMNN=UserLatestMsgNumNotify;

class ULMNN_Manager{
    constexpr static char *ULMNN_DIR="data/user_latest_msg_num_notify/";
    private:    
        static ULMNN_Manager *instance;
        ULMNN_Manager();

        unordered_map<int, UserLatestMsgNumNotify> user_latest_msg_num_notify_map;

        mutex user_latest_msg_num_notify_map_mutex; // 用户通知锁
        
        string get_user_latest_msg_num_notify_file_path(int user_id);

        void create_new_file(string file_pat,int user_id);
        void save_to_file(int user_id, const UserLatestMsgNumNotify& user_latest_msg_num_notify);
        void load_from_file(int user_id, UserLatestMsgNumNotify& user_latest_msg_num_notify); 
        void save_all_user_latest_msg_num_notify();

    public:
        ULMNN& QuickLoadUserLatestMsgNumNotify(int user_id);

        void add_one_num(int user_id, string chat_room_id, string time);

        
        void send_latest_msg_num(int user_id, int socket_fd);

        void SendOrSaveUserLatestMsgNumNotify_regularly(int clear_frequency);

        void add_num_for_user_in_chat_room(int user_id, string chat_room_id, string time);

        static ULMNN_Manager* get_instance();



        #ifdef DEBUG
        void save_all_user_latest_msg_num_notify_debug(){
            save_all_user_latest_msg_num_notify();
        }

        void print_user_latest_msg_num_notify_map(int user_id){
            auto& user_latest_msg_num_notify = QuickLoadUserLatestMsgNumNotify(user_id);
            cout<<user_latest_msg_num_notify.serialize().dump(4)<<endl;
        }
        #endif  
};